using Microsoft.EntityFrameworkCore;

namespace WeatherApp.Models
{
    public class WeatherDbContext : DbContext
    {
        public WeatherDbContext(DbContextOptions<WeatherDbContext> options) : base(options)
        {
        }

        public DbSet<WeatherRecord> WeatherRecords { get; set; }
        public DbSet<UserSettings> UserSettings { get; set; }
        public DbSet<SearchHistory> SearchHistories { get; set; }
        public DbSet<FavoriteCity> FavoriteCities { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // WeatherRecord 配置
            modelBuilder.Entity<WeatherRecord>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.City).IsRequired().HasMaxLength(100);
                entity.Property(e => e.Weather).HasMaxLength(50);
                entity.Property(e => e.Temperature).HasMaxLength(20);
                entity.Property(e => e.Humidity).HasMaxLength(20);
                entity.Property(e => e.WindDirection).HasMaxLength(50);
                entity.Property(e => e.WindPower).HasMaxLength(20);
                entity.Property(e => e.AirQuality).HasMaxLength(20);
                entity.Property(e => e.UvIndex).HasMaxLength(20);
                entity.Property(e => e.Visibility).HasMaxLength(20);
                entity.Property(e => e.Pressure).HasMaxLength(20);
                entity.Property(e => e.FeelsLike).HasMaxLength(20);
                entity.Property(e => e.QueryTime).IsRequired();
            });

            // UserSettings 配置
            modelBuilder.Entity<UserSettings>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.UserId).IsRequired().HasMaxLength(100);
                entity.Property(e => e.TemperatureUnit).HasMaxLength(20);
                entity.Property(e => e.Language).HasMaxLength(10);
                entity.Property(e => e.Theme).HasMaxLength(20);
                entity.HasIndex(e => e.UserId).IsUnique();
            });

            // SearchHistory 配置
            modelBuilder.Entity<SearchHistory>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.UserId).IsRequired().HasMaxLength(100);
                entity.Property(e => e.CityName).IsRequired().HasMaxLength(100);
                entity.Property(e => e.SearchTime).IsRequired();
            });

            // FavoriteCity 配置
            modelBuilder.Entity<FavoriteCity>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.UserId).IsRequired().HasMaxLength(100);
                entity.Property(e => e.CityName).IsRequired().HasMaxLength(100);
                entity.Property(e => e.AddTime).IsRequired();
            });
        }
    }

    // 天气记录实体
    public class WeatherRecord
    {
        public int Id { get; set; }
        public string City { get; set; } = string.Empty;
        public string Weather { get; set; } = string.Empty;
        public string Temperature { get; set; } = string.Empty;
        public string Humidity { get; set; } = string.Empty;
        public string WindDirection { get; set; } = string.Empty;
        public string WindPower { get; set; } = string.Empty;
        public string AirQuality { get; set; } = string.Empty;
        public string UvIndex { get; set; } = string.Empty;
        public string Visibility { get; set; } = string.Empty;
        public string Pressure { get; set; } = string.Empty;
        public string FeelsLike { get; set; } = string.Empty;
        public DateTime QueryTime { get; set; }
    }

    // 搜索历史实体
    public class SearchHistory
    {
        public int Id { get; set; }
        public string UserId { get; set; } = string.Empty;
        public string CityName { get; set; } = string.Empty;
        public DateTime SearchTime { get; set; }
    }

    // 收藏城市实体
    public class FavoriteCity
    {
        public int Id { get; set; }
        public string UserId { get; set; } = string.Empty;
        public string CityName { get; set; } = string.Empty;
        public DateTime AddTime { get; set; }
    }
} 